package springboot.study.project.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import springboot.study.project.common.Result;
import springboot.study.project.dto.BulletinDto;
import springboot.study.project.model.Bulletin;
import springboot.study.project.service.BulletinService;
import springboot.study.project.util.common.ResultUtil;
import springboot.study.project.util.common.StringUtil;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: BulletinController
 * @Description: 公告控制器
 * @Author: liu-hao
 * @Date: 2021-03-31 22:45
 * @Version: 1.0
 **/
@RestController
@RequestMapping("/bulletin")
public class BulletinController {
    @Autowired
    BulletinService bulletinService;

    @RequestMapping("/pushBulletin")
    public Result pushBulletin(@RequestBody Bulletin bulletin, HttpServletRequest request) {
        if (bulletin == null) {
            return ResultUtil.failure("添加公告失败！");
        } else if (StringUtil.isBlank(bulletin.getBulletinTitle())) {
            return ResultUtil.failure("公告标题不能为空！");
        } else if (StringUtil.isBlank(bulletin.getBulletinContent())) {
            return ResultUtil.failure("公告内容不能为空！");
        }
        if (!bulletinService.pushBulletin(bulletin, request)) {
            return ResultUtil.failure("添加公告失败！");
        } else {
            return ResultUtil.success();
        }
    }

    @RequestMapping("/upload")
    public Result handleFileUpload(@RequestParam("file") MultipartFile file) {

        if (file.isEmpty()) {
            return ResultUtil.failure("文件为空");
        }
        // 获取文件名
        String fileName = file.getOriginalFilename();
        System.out.println("上传的文件名为：" + fileName);
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        System.out.println("上传的后缀名为：" + suffixName);
        // 文件上传后的路径
        String filePath = System.getProperty("user.dir") + "/src/main/resources/static/upload/" + fileName;
        File dest = new File(filePath);
        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
            return ResultUtil.success("/upload/" + fileName);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResultUtil.failure("上传失败");
    }

    @RequestMapping("/removeBulletin")
    public Result removeBulletin(@RequestParam(name = "bulletinId") String bulletinId) {
        boolean result = bulletinService.removeBulletin(bulletinId);
        if (result) {
            return ResultUtil.success();
        } else {
            return ResultUtil.failure("公告删除失败");
        }
    }

    @RequestMapping("/modifyBulletin")
    public Result modifyBulletin(@RequestBody Bulletin bulletin) {
        boolean result = bulletinService.modifyBulletin(bulletin);
        if (result) {
            return ResultUtil.success();
        } else {
            return ResultUtil.failure("公告修改失败");
        }
    }

    @RequestMapping("/getHotBulletins")
    public Result getHotBulletins() {
        List<Bulletin> hotBulletins = bulletinService.getHotBulletins();
        return ResultUtil.success(hotBulletins);
    }

    @RequestMapping("/getBulletinById")
    public Result getBulletinById(@RequestParam(name = "bulletinId") String bulletinId) {
        Bulletin bulletin = bulletinService.getBulletinById(bulletinId);
        return ResultUtil.success(bulletin);
    }

    @RequestMapping("/getBulletins")
    public Map<String, Object> getBulletins(@RequestParam(name = "bulletinTitle") String bulletinTitle, // 公告标题
                                            @RequestParam(name = "publisherName") String publisherName, // 发布者姓名
                                            @RequestParam(name = "dateRange") String dateRange, // 日期范围
                                            @RequestParam(name = "page", defaultValue = "1") int pageNum,
                                            @RequestParam(name = "limit", defaultValue = "10") int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        Map<String, Object> hotBulletins = bulletinService.getBulletins(bulletinTitle, publisherName, dateRange, offset, pageSize);
        return hotBulletins;
    }
}
